package com.changli02.filestream;

import org.junit.Test;

import java.io.*;

public class filerReaderWriterTest {
    @Test
    public void test() throws IOException {

        File file = new File("D:\\后端\\File_ioStream\\hello.txt");
        FileReader fr = new FileReader(file);
        int data = fr.read();
        while (data != -1) {
            System.out.print((char) data);
            data = fr.read();
        }
        fr.close();
    }




    @Test
    public void test2() {
        File file = new File("D:\\后端\\File_ioStream\\hello.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            int data = fr.read();
            while (data != -1) {
                System.out.print((char) data);
                data = fr.read();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }






    @Test
    public void test3() {
        File file = new File("D:\\后端\\File_ioStream\\hello.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            char [] buffer = new char[(int)file.length()];
            int len ;
            while ((len = fr.read(buffer)) != -1) {
                for (int i = 0; i < len; i++) {
                    System.out.print(buffer[i]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    @Test
    public void test4() {
        File file = new File ("D:\\后端\\File_ioStream\\info.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(file,true);
            fw.write("I love u ,ZKY\n");
            fw.write("Hope you to be happy and healthy everyday\n");

            System.out.println("输出成功！");




        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if(fw != null) {
                    fw.close();

                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }



    }
    @Test
    public void test5() {
        File file1 = new File ("D:\\后端\\File_ioStream\\hello.txt");
        File file2 = new File ("D:\\后端\\File_ioStream\\hello_copy.txt");

        FileReader fr = null;
        FileWriter fw = null;

        try {
            fr = new FileReader(file1);
            fw = new FileWriter(file2);
            char [] buffer = new char[(int)file1.length()];
            int len ;
            while ((len= fr.read(buffer)) !=-1){
                fw.write(buffer,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }






    }
}
